﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FormControllers;
using AgencyEntityLib.BaseEntity;
using Ecopsy.Tools;
using Ecopsy.DataTier;
using SqlLibrary;


public partial class iFarmeAdmin_SiteMap : iAdmin_Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) MyDataBind();
    }
  
    protected void AddButton_Click(object sender, EventArgs e)
    {
        Response.Redirect("iFarmeAdmin_EditSiteMap.aspx?"+qSiteMapItemID+"="+ GlobalConstants.qPrefNEW_RECORD+Guid.NewGuid ().ToString ()   );
    }

    protected string GetEditCommand(string ID)
    {
        string sUrl="iFarmeAdmin_EditSiteMap.aspx?" + qSiteMapItemID + "=" + ID;
        return "Redirect('" + sUrl + "');";
    }
   
    protected string GetRoles(string ID)
    {
        return "Роли";
    }
  
    protected void OnDeleteClick(object sender, EventArgs e)
    {
        //ImageButton btn = (ImageButton)sender;
        //GlobalConstants.CurrentAgency.SurfaceGroups.Remove(btn.CommandArgument);

        //wsAgency ws = new wsAgency();
        //ws.SaveAgency();
        //MyDataBind();

    }

    Agency Agency { get { return GlobalConstants.CurrentAgency; } }
   
    public SiteMapItem [] GetList(int startRowIndex, int maximumRows, out int RowCount)
    {
        SiteMapItem [] r= GlobalConstants.CurrentAgency.SitMap.ToArray<SiteMapItem>();    
        //заряжаем comparer функцией сортировки 
        ListWhere w = new ListWhere();
        w.SortCondition.fnOrderBy = SortFunctions.fOrderBy;
        // Сортируем        
        Array.Sort(r, w.SortCondition);
        RowCount = int.MaxValue;
        return r;
    }

    private void MyDataBind()
    {
        ObjectDS_PF ODS = new ObjectDS_PF(GetList);
        lvDiscounts.DataSource = ODS.ImAsObjectDataSource;
        DataBind();

        // проставить статус редактирования        
        SetAlertForTextBox(this.Controls, "CheckBox", "OnCheckedChanged", "ShowAlert()");

    }

    protected string GetStateVisible()
    {        
        if (Agency.State != EntityState.Loaded) return string.Empty;
        return "hidden";

    }

    protected void SaveButton_Click(object sender, EventArgs e)
    {
        foreach (ListViewDataItem item in lvDiscounts.Items)
        {
            CheckBoxList chList = (CheckBoxList)item.FindControl("chRoleList");
            HiddenField hfID = (HiddenField)item.FindControl("hfID");
            UpadetMapItem(chList, hfID);

        }
        wsAgency ws = new wsAgency();
        ws.SaveAgency();
    }

    private void UpadetMapItem(CheckBoxList chList, HiddenField hfID)
    {

        SiteMapItem item = (SiteMapItem)Agency.SitMap[hfID.Value];
        item.Roles.Clear();
        foreach (ListItem i in chList.Items)
        {
            if (i.Selected) item.Roles.Add(new EntitySingle() { ID = i.Value });
        }

    }

    protected void lvDiscounts_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        SiteMapItem item = (SiteMapItem)(((System.Web.UI.WebControls.ListViewDataItem)(e.Item)).DataItem);
        CheckBoxList chList = (CheckBoxList)e.Item.FindControl("chRoleList");

        ListItemCollection coll = GetEmplRoles(item);
        foreach (ListItem itm in coll)
        {
            ListItem l = new ListItem();
            l.Value = itm.Value;
            l.Text = itm.Text;
            l.Selected = itm.Selected;            chList.Items.Add(l);
        }

    }

    private ListItemCollection GetEmplRoles(SiteMapItem SiteMapItem)
    {
        ListItemCollection res = new ListItemCollection();

        ////список ролей
        ws_Securety ws = new ws_Securety();
        RoleList rl = ws.GetRoleList();
        foreach (DictionaryItem item in rl)
        {
            ListItem i = new ListItem();
            i.Value = item.ID;
            i.Text = item.Value.ToString();
            i.Selected = (null != SiteMapItem.Roles[item.ID]);
            res.Add(i);
        }
        return res;
    }

    protected string GetPageType(string PageTypeId)
    {
        wsDictionary ws = (wsDictionary)GlobalConstants.CurrentAgency.WS_FactoryList[WS_FactoryList.wsKey_Dictionary];
        List<DictionaryItem> l=   ws.GetDictionary(GlobalConstants.DictionaryPageType);
        foreach (DictionaryItem i in l)
        {
            if (i.ID == PageTypeId) return i.Value.ToString (); 
        }
                return string.Empty ;
    }
  
}
